package com.noble.meether.ui.vip;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.ui.user.paygoods.GoodsConstant;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.image.ImageLoader;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.library.utils.BitmapUtil;
import com.noble.library.utils.ScreenUtils;
import com.noble.library.utils.UIUtil;
import com.noble.library.widget.RecyclerView.base.BaseRecyclerViewHolder;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.ui.vip.adapter.PrivilegeAdapter;
import com.noble.meether.ui.vip.adapter.VipAdapter;
import com.noble.meether.ui.vip.bean.PrivilegeList;
import com.noble.meether.ui.vip.bean.Vip;
import com.noble.meether.ui.vip.bean.VipList;

/**
 * 我的VIP
 */
public class MyVipAct extends BaseAppCompatActivity implements View.OnClickListener, RequestComplete, PObserver, BaseRecyclerViewHolder.OnItemClickListener {
    private float toDpMutliple = 1; //根据屏幕密度获取屏幕转换倍数
    private ImageView user_head;
    private TextView user_nick, user_vip;

    private RecyclerView privilegeList;         // 特权列表
    private PrivilegeAdapter privilegeAdapter;

    private RecyclerView vipList;               // 充值列表
    private VipAdapter vipAdapter;
    private VipList list;
    private Vip vip;          // 选中VIP item

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_vip);
        initView();
    }

    private void initView() {
        setBackView("我的VIP");
        toDpMutliple = ScreenUtils.toDpMultiple(this);

        ImageView imgBg = findViewById(R.id.iv_bg);
        Bitmap bitmap = BitmapUtil.getDecodeBitmap(this, R.drawable.bg_vip, 320, 480);
        UIUtil.setBackground(imgBg, new BitmapDrawable(null, bitmap));

        user_head = findViewById(R.id.user_head);
        user_nick = findViewById(R.id.user_nick);
        user_vip = findViewById(R.id.user_vip);
        findViewById(R.id.vip_open).setOnClickListener(this);
        refreshView();

        privilegeList = findViewById(R.id.privilege_list);
        privilegeList.setLayoutManager(new GridLayoutManager(this, 2));
        privilegeList.addItemDecoration(new ItemSpaces((int) (15 * toDpMutliple)));

        vipList = findViewById(R.id.vip_list);
        vipList.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        vipList.addItemDecoration(new ItemSpaces((int) (10 * toDpMutliple)));

        initData();
        MsgMgr.getInstance().attach(this);
    }

    private void initData() {
        privilegeAdapter = new PrivilegeAdapter(this);
        privilegeList.setAdapter(privilegeAdapter);

        vipAdapter = new VipAdapter(this);
        vipList.setAdapter(vipAdapter);
        vipAdapter.setOnItemClickListener(this);

        ModuleMgr.getCommonMgr().reqVipPrivilege(this);  // 特权列表
        ModuleMgr.getCommonMgr().reqVipList(this);
    }

    private void refreshView() {
        UserDetail userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        ImageLoader.loadCircleAvatar(this, userDetail.getAvatar(), user_head);
        user_nick.setText(userDetail.getNickname());
        if (userDetail.isForEverVip()) {
            user_vip.setText("永久VIP会员");
        } else if (userDetail.isVip()) {
            user_vip.setText("VIP到期时间：" + userDetail.getVipEndTime());
        }
    }

    @Override
    public void onItemClick(View convertView, int position) {
        if (list != null && list.getVipList().size() > position) {
            this.vip = list.getVipList().get(position);
        }
        vipAdapter.setSelected(position);
        vipAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.vip_open:     // 开通VIP
                if (list == null || list.getVipList().size() <= 0) {
                    PToast.showShort("未成功获取数据，请退出重试");
                    return;
                }
                if (vip == null) {
                    vip = list.getVipList().get(0);
                }
                MeetUIShow.showPayTypeDlg(this, GoodsConstant.PAY_VIP, vip.getId(), "开通" + vip.getDescribe() + "VIP");
                break;
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqVipPrivilege) {
            if (!response.isOk()) return;
            PrivilegeList list = (PrivilegeList) response.getBaseData();
            if (list.getPrivilegeList().size() <= 0) return;
            privilegeAdapter.setList(list.getPrivilegeList());
            return;
        }

        if (response.getUrlParam() == UrlParam.reqVipList) {
            if (!response.isOk()) return;
            list = (VipList) response.getBaseData();
            if (list.getVipList().size() <= 0) return;
            vipAdapter.setList(list.getVipList());
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                refreshView();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    private class ItemSpaces extends RecyclerView.ItemDecoration {
        private int space;

        public ItemSpaces(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.right = space / 2;
            outRect.left = space / 2;
            outRect.top = space;
        }
    }
}
